const express = require("express");
const mongoose = require("mongoose");

const MODEL = require("../../models/Connect");

const router = express.Router({
  mergeParams: true,
});

const _model = MODEL;

// 创建预约
router.post("/", async (req, res) => {
  const model = await _model.create(req.body);
  res.send(model);
});

// 车主操作
// router.put("/user/aggree/:connect_id", async (req, res) => {
//   const model = await _model.findByIdAndUpdate(req.params.id, req.body);
//   res.send(model);
// });

// 判断用户是否预约此车辆
// fromId: mongoose.Types.ObjectId(req.query.fromId),
// toId: mongoose.Types.ObjectId(req.query.toId),
// carId: mongoose.Types.ObjectId(req.query.carId),
router.get("/is_connect", async (req, res) => {
  // global.console.log(req.query);
  const item = await _model
    .where({
      fromId: mongoose.Types.ObjectId(req.query.fromId),
      toId: mongoose.Types.ObjectId(req.query.toId),
      carId: mongoose.Types.ObjectId(req.query.carId),
      isVaild: true,
    })
    .findOne();

  // global.console.log(item);

  const item_2 = await _model
    .where({
      fromId: mongoose.Types.ObjectId(req.query.fromId),
      toId: mongoose.Types.ObjectId(req.query.toId),
      carId: mongoose.Types.ObjectId(req.query.carId),
    })
    .findOne();

  // global.console.log(item_2);

  if (item != null) {
    res.send({
      is_connect: true,
      state: item.state,
      data: item,
    });
  } else {
    if (item_2 != null) {
      res.send({
        is_connect: true,
        state: item_2.state,
        data: item_2,
      });
    } else {
      res.send({
        is_connect: false,
      });
    }
  }
});

// 用户查看预约情况
router.get("/user/:userId", async (req, res) => {
  const from_users = await _model.aggregate([
    {
      $match: {
        fromId: mongoose.Types.ObjectId(req.params.userId),
      },
    },
    {
      $lookup: {
        from: "users",
        localField: "fromId",
        foreignField: "_id",
        as: "from_user",
      },
    },
    {
      $lookup: {
        from: "users",
        localField: "toId",
        foreignField: "_id",
        as: "to_user",
      },
    },
    {
      $lookup: {
        from: "cars",
        localField: "carId",
        foreignField: "_id",
        as: "car",
      },
    },
  ]);

  const to_users = await _model.aggregate([
    {
      $match: {
        toId: mongoose.Types.ObjectId(req.params.userId),
      },
    },
    {
      $lookup: {
        from: "users",
        localField: "fromId",
        foreignField: "_id",
        as: "from_user",
      },
    },
    {
      $lookup: {
        from: "users",
        localField: "toId",
        foreignField: "_id",
        as: "to_user",
      },
    },
    {
      $lookup: {
        from: "cars",
        localField: "carId",
        foreignField: "_id",
        as: "car",
      },
    },
  ]);

  res.send({
    from_users,
    to_users,
  });
});

// 查看预约状态
router.get("/get_state", async (req, res) => {
  const items = await _model.find();
  res.send(items);
});

// router.get("/:id", async (req, res) => {
//   const items = await _model.find();
//   res.send(items);
// });

router.put("/:id", async (req, res) => {
  const model = await _model.findByIdAndUpdate(req.params.id, req.body);
  res.send(model);
});

router.delete("/:id", async (req, res) => {
  await _model.findByIdAndDelete(req.params.id, req.body);
  res.send({
    success: true,
  });
});

// router.get("/", async (req, res) => {
//   const queryOptions = {};
//   const items = await _model.find().setOptions(queryOptions);
//   res.send(items);
// });

module.exports = router;
